using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.IFilter
{
    [Serializable]
    public class FileContentSearchOutput : ResultBase
    {
        private List<string> _SearchResults;

        public List<string> SearchResults
        {
            get { return _SearchResults; }
            set { _SearchResults = value; }
        }

        public FileContentSearchOutput()
            : base(Guid.Empty, Guid.Empty)
        {
            this._SearchResults = null;
        }

        public FileContentSearchOutput(Guid componentID, Guid jobTicketID, List<string> searchResults)
            : base(componentID, jobTicketID)
        {
            this._SearchResults = searchResults;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            // XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlNode resultNode = XmlDataUtil.AddElement(ref xDoc, xNode, "SearchResults");
            if (this.SearchResults != null)
            {
                foreach (string filePath in this.SearchResults)
                {
                    XmlNode fileNode = XmlDataUtil.AddElement(ref xDoc, resultNode, "File");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fileNode, "FilePath", filePath);
                }
            }

            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileContentSearchOutput output = new FileContentSearchOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.SearchResults = null;
                XmlNodeList fileNodes = xNode.SelectNodes("SearchResults/File");
                if (fileNodes != null && fileNodes.Count > 0)
                {
                    output.SearchResults = new List<string>();
                    foreach (XmlNode fileNode in fileNodes)
                    {
                        string filePath = fileNode.Attributes.GetNamedItem("FilePath").Value;
                        output.SearchResults.Add(filePath);
                    }
                }
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}